<?php
/**
 *@file messagesystem.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-10-19 下午6:15:47
 *@brief  
 */
namespace haibao\www\business\message;

use haibao\www\data\message\MessageBox as MessageBoxData;
use haibao\www\data\message\MessageSystem as MessageSystemData;
use haibao\www\cache\Message as MessageCache;

class MessageSystem extends \haibao\www\business\Base{
    
	public static function addMessage($model){
		$messageData = new MessageSystemData();
		$messageData->add($model);
	}
	
	public static function getMessageList($articleFilter){
		$messageData = new MessageSystemData();
		return $messageData->getAll($articleFilter);
	}
	
	public static function addMessageBox($messageBoxModel){
		$messageBoxData = new MessageBoxData();
		\haibao\www\business\message\MessageUser::updateMessageUser($messageBoxModel->ToUserId,$messageBoxModel->MessageType);
		$messageBoxData->add($messageBoxModel);
	}
	
	/**
	 * 用户登录后同步系统消息到MessageBox中
	 */
	public static function syncMessage($userId){

		// 将用户添加任务队列
		\haibao\www\cache\Message::pushMessageUser($userId);
		// 添加一个拉取系统消息的标记位 防止多个请求数据重复的问题
		/* if(MessageCache::getPullSystemFlag($userId)){
			return;
		}
	    $lastSyncTime = \haibao\www\business\message\MessageUser::getLastSyncTimeByUserId($userId);
	    $systemMessage = self::globalSystemMessage($lastSyncTime);
	    $models = array();
	    if($systemMessage){
	        foreach($systemMessage as $message){
	            $boxModel = new \haibao\www\model\data\message\MessageBox();
	            $boxModel->ToUserId = $userId;
	            $boxModel->MessageType = \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_SYSTEM;
	            $boxModel->MessageData = $message['MessageContent'];
	            $boxModel->IsRead = \haibao\www\model\data\message\MessageBox::READ_NOT;
	            $boxModel->Status = \haibao\www\model\data\message\MessageBox::STATUS_ENABLE;
	            $boxModel->CreateTime = new \DateTime();
	            array_push($models, $boxModel);
	        }
	        $boxData = new MessageBoxData();
	        $boxData->addAll($models);
	        \haibao\www\business\message\MessageUser::updateMessageUser($userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_SYSTEM, true, count($models));
	    }
	    MessageCache::delPullSystemFlag($userId); */
	}
	
	/**
	 * 根据上次阅读时间获取系统消息
	 */
	private static function globalSystemMessage($lastSyncTime){
	    $systemData = new \haibao\www\data\message\MessageSystem();
	    return $systemData->globalSystemMessage($lastSyncTime);
	}
	
	/**
	 * 获取用户系统发送的消息
	 */
	public static function getSystemMessageByUserId($messageUser){
	
		$systemMessage = self::globalSystemMessage($messageUser->LastReadTime->date);
		$models = array();
		if($systemMessage){
			foreach($systemMessage as $message){
			$boxModel = new \haibao\www\model\data\message\MessageBox();
			$boxModel->ToUserId = $messageUser->UserId;
			$boxModel->MessageType = \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_SYSTEM;
			$boxModel->MessageData = $message['MessageContent'];
			$boxModel->IsRead = \haibao\www\model\data\message\MessageBox::READ_NOT;
			$boxModel->Status = \haibao\www\model\data\message\MessageBox::STATUS_ENABLE;
			$boxModel->CreateTime = new \DateTime();
			array_push($models, $boxModel);
			}
			$boxData = new MessageBoxData();
			$boxData->addAll($models);
		}
		return count($models);
	}
}